package com.aepryus.loom;

import java.util.Date;
import java.text.*;
import java.sql.*;
import com.aepryus.util.*;

public class OraclePersistence extends Persistence {
	public OraclePersistence(String dbClass,String dbURL,String username,String password) throws PersistenceException {
		super(dbClass, dbURL, username, password);
	}

// Persistence ====================================================================================
    public String toSQL (String value) {
        if (value == null) return "null";
        StringBuffer sb = new StringBuffer();
        sb.append('\'');
        int j=0;
        int i=value.indexOf('\'');
        for (;i!=-1;i=value.indexOf('\'',j)) {
            sb.append(value.substring(j,i+1));
            sb.append('\'');
            j = i+1;
        }
        sb.append(value.substring(j));
        sb.append('\'');
        return sb.toString();
    }

    public String toSQL (Date value) {
		return value==null ? "null" : "'"+dateFormat.format(value)+"'";
	}
	public Date toDate (Object value) {
		if (value == null)
			return null;
		return new Date(((Timestamp)value).getTime());
	}

	public String toSQL (DateTime value) {
		return value==null ? "null" : "to_date('"+timeFormat.format(value)+"','dd-mon-yyyy HH24:mi:ss')";
	}
	public DateTime toDateTime (Object value) {
		if (value == null)
			return null;
		return new DateTime(((Timestamp)value).getTime());
	}

// Static =========================================================================================
	private static DateFormat dateFormat = new SimpleDateFormat("dd-MMM-yyyy");
	private static DateFormat timeFormat = new SimpleDateFormat("dd-MMM-yyyy HH:mm:ss");
}
